﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using EqRepair.Code.Classes;
using EqRepair.Code.Data;

namespace EqRepair.Web
{
	public partial class ReportOrd : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void Button1_Click(object sender, EventArgs e)
		{
			try
			{
				using (var db = new DataClasses2DataContext())
				{
					DateTime d1, d2;
					if ((Calendar1.SelectedDate) != new DateTime(1, 1, 1, 0, 0, 0))
						d1 = Calendar1.SelectedDate;
					else
						d1 = new DateTime(DateTime.Now.Year, 1, 1);
					if (Calendar2.SelectedDate != new DateTime(1, 1, 1, 0, 0, 0))
						d2 = Calendar2.SelectedDate;
					else
						d2 = new DateTime(DateTime.Now.Year + 1, 1, 1);
					if (DropDownList1.SelectedItem != null)
					{
						AccessData.CreateAutoReq();
						int i = 0;
						var Join =
							from lnk in db.RequestForRepairs
							join eq in db.EquipmentInOrganizations on lnk.EquipmentInOrganization equals eq

							where (eq.LastRepairDate.Value > d1 && eq.LastRepairDate.Value < d2 &&
							       (eq.OrganizationId == Int32.Parse(DropDownList1.SelectedValue)) &&
							       (lnk.Status == 3))
							select new
							       	{
							       		//пп = 1,
							       		r1 = AccessData.GetIDFullParent(lnk.EquipmentInOrganization),
							       		r2 = eq.Equipment.Name,
							       		r3 = eq.LastRepairDate,
							       		r4 = eq.Equipment.LifeTime,
							       		r5 = eq.Equipment.RepairTime,
							       		r6 = eq.Equipment.RepairCost
							       	};
						GridView1.DataSource = Join;
						GridView1.DataBind();
						double t = 0, m = 0;
						foreach (GridViewRow row in GridView1.Rows)
						{
							t += double.Parse(row.Cells[4].Text);
							m += double.Parse(row.Cells[5].Text);
						}



						sum.Text = "Итого трудозатрат (чел./час): " + t + ". Стоимость работ: " + m;
					}
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show("Произошла непредвиденная ошибка. " +ex.Message, "Внимание.", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
			}
		}
	}
}
